//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')
const lib = Pager.lib
const util = require('../../utils/util.js')

const simulationScore = [
  { title: '您喜欢吃苹果么', type: '喜好', level_list: ["不喜欢", "还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 }] },
  { title: '您喜欢吃香蕉么', type: '口味', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 }] },

  { title: '您喜欢吃梨么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },

  { title: '您喜欢吃葡萄么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },

  { title: '您喜欢吃火龙果么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },
  { title: '您喜欢吃番石榴么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },

  { title: '您喜欢吃柠檬么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },

  { title: '您喜欢吃荔枝么', type: '喜好', level_list: ["不喜欢","还行","特么喜欢"], checklist: [{ "title": "不喜欢", "value": 1 }, { "title": "不喜欢", "value": 1.5 }, { "title": "不喜欢", "value": 2 }, { "title": "还行", "value": 2.5 }, { "title": "还行", "value": 3 }, { "title": "特么喜欢", "value": 3.5 }, { "title": "特么喜欢", "value": 4 }, { "title": "特么喜欢", "value": 4.5 }, { "title": "特么喜欢", "value": 5 } ] },
]


let datax = []
const $pgSize = 2
for(var i=0; i <simulationScore.length; i+= $pgSize){
  datax.push(simulationScore.slice(i, i+$pgSize));
}

Pager({
  data: {
    score: {
      $$id: 'score',
      data: adpapterScore(datax[0], {total: 8, current_no: 0}),
      itemClass: 'mb-default score-item bg-fff size22 color-grey',
    },
    pageBtn: {
      $$id: 'pageBtn',
      title: [
        {
          title: ' ',
          itemClass: 'icon-arrow-right-circle icon-left small score-absolute',
          tap: 'prevPage',
          'hover-class': 'hover'
        },
        {
          title: ' ',
          itemClass: 'icon-arrow-right-circle mt-20-r',
          tap: 'onShowDifPic',
          // tap: 'nextPage',
          'hover-class': 'hover'
        }
      ],
      titleClass: 'flex-row-center-center mt-10-p ss-relative',
      itemClass: 'bg-fff',
      itemClass: "btn-list-default"
    }
  },
  storeData: [],
  currentIdx: 0,
  sliderChange: function (e, param, inst) {
    const {inputData, profile, id} = param
    const num = id.substr(-1, 1)      //获取到ii
    const $gradeNum = this.getElementsById('grade-num'+num)      //分数
    const $gradeArrow = this.getElementsById('grade-arrow'+num)     //箭头
    let val =  inputData.value - 1
    $gradeNum.update({title: inputData.value.toString()})
    $gradeArrow.css('left: calc(100% / 9 * 2 *'+ val +')')
  },
  pageCommon: function( $current){
    const that = this
    const $score = this.getElementsById('score')    //列表
    const $pgbtn = this.getElementsById('pageBtn')      //按钮
    this.currentIdx = $current
    const _scoreData = adpapterScore(datax[$current], {total: 8, current_no: $current, values: that.storeData[$current] || []})
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      wx.hideLoading()
      $score.update(_scoreData)
      if ($current > 0 ){
        !$pgbtn.hasClass('active') && $pgbtn.addClass('active')
      }
      else {
        $pgbtn.removeClass('active')
      }
    }, 200)
  },
  isLast: false,
  nextPage: util.throttle(function (e, param, inst) {
    const that = this

    let $currentNum = this.currentIdx
    const $sliderCxGetData = this.getElementsById('grade-num0').getData()    //实时值1  
    const $sliderCx2GetData = this.getElementsById('grade-num1').getData()    //实时值2

    let $sliderCxTitle = Number($sliderCxGetData.title)   //滑动1   
    let $sliderCxTitle2 = Number($sliderCx2GetData.title)    //滑动2  

    let _findIdx = this.storeData.findIndex((item,idx) =>idx===$currentNum)
    if ( _findIdx > -1) {
      this.storeData[_findIdx] = [
        {value: $sliderCxTitle},
        {value: $sliderCxTitle2},
      ]
    }
    else {
      this.storeData.push([
        {value: $sliderCxTitle},
        {value: $sliderCxTitle2},
      ])
    }
    $currentNum = $currentNum + 1
    if ($currentNum < datax.length){
      this.isLast = false
      this.pageCommon($currentNum)
    }
    else {
      this.isLast = true
    }
    if (this.isLast) {
      let _total = 0
      for(let i= 0; i< that.storeData.length; i++) {
        for(let ii = 0; ii < that.storeData[i].length; ii++) {
          _total += that.storeData[i][ii].value
        }
      }
      wx.showModal({
        title: '总分',
        content: _total.toString(),
      })
    }
  }),
  prevPage: function (e, param, inst) {
    let $prevNum = this.currentIdx - 1 > -1 ? this.currentIdx - 1 : 0
    this.pageCommon( $prevNum)
  },
  isAd: false,
  onShowDifPic: function(e, params, inst) {
    const that = this
    if (!this.isAd) {
      util.stimulateAD('adunit-0c409d585b8ce18c', function() {
        that.isAd = true
        that.nextPage(e, params, inst)
      })
    }
    else {
      that.nextPage(e, params, inst)
    }
  }
})

function checklistFunc(res, type) {
  return res.map( (item, ii) => {
    return {
      title: type == 'number' ? item.value : item,
      attr: type == 'number' ? {title: item.title} : {},
      itemClass: type !== 'number' ? `'plr-default ' + ${ii === 1 ? 'ss-center' : ii === 2 ? 'ss-right' : ''} ${ii != 0 ? ' bl-default' : ''}` : ii != 0 ? ' bl-default' : ''
    }
  })
}
function adpapterScore (data, param) {
  const {total, current_no, values = []} = param
  return data.map( (item, ii) => {
    let _c_p = current_no * 2 + ii + 1
    let _val = values[ii] ? values[ii].value : 2.5
    return {
      title: [
        {
          $$id: 'grade-num'+ii,
          title: _val,
          attr: {type: item.type, current_no: _c_p ,total: total, score_list: item.checklist},
          itemClass: "fw-bold color-primary wid-160-r flex-row-center-center hei-p100 size72"
        },
        {
          title: [
            { title: item.type+' '+_c_p+'/'+total },
            { title: item.title, itemClass: 'size32 color-000' }
          ],
          titleClass: 'flex-column-center pl-default scrore-title',
          itemClass: 'bl-default hei-p100'
        },
      ],
      body: checklistFunc(item.level_list),
      dot: [
        {
          '@item': {
            $$id: 'grade-arrow'+ii,
            title: ' ',
            itemStyle: _val ? 'left: calc(100% / 9 * 2 *'+ (_val - 1) +')' : 'left: 0px',
            itemClass: 'icon-arror-top-solid absolute',
          },
          li: checklistFunc(item.checklist, 'number'),
          liClass: 'flex-row-center next-flex-1 color-666 fw-bold ss-center',
          itemClass: 'ss-relative bg-default size26 hei-70-r-x bb-default'
        },
        {
          '@form': {
            $$id: 'grade-x'+ii,
            data: [
              {
                title: '',
                input: {
                  type: 'slider',
                  id: 'grade'+ii,
                  value: _val,
                  max: 5,
                  step: 0.5,
                  min: 1,
                  bindchanging: 'sliderChange',
                  'block-size': 24,
                  'block-color': '#fff',
                  'slider-left': 60,
                  activeColor: '#C10000',
                  itemClass: 'score-padding'
                },
              }
            ],
          },
          itemClass: 'p-default'
        }
      ],
      // attr: {id: ii > pgSize * pgNum ? pgNum = pgNum + 1 : pgNum},
      bodyClass: 'flex-row-center next-flex-1 bb-default bt-default color-grey size11 hei-70-r-x',
      titleClass: 'flex-row hei-166-r bt-default next-ui-item-display'
    }
  })
}